package com.hbx.eduservice.controller;


import com.hbx.commonutils.R;
import com.hbx.eduservice.client.VodClient;
import com.hbx.eduservice.entity.EduVideo;
import com.hbx.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2023-10-02
 */
@Api(tags = {"小节管理"})
@CrossOrigin
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    // 后面这个方法需要完善，删除小节时，视频也要删除
    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable("id") String id){
        //根据小节id查询视频id
        EduVideo video = videoService.getById(id);
        String sourceId = video.getVideoSourceId();

        if(!sourceId.isEmpty()){
            //根据视频id，远程调用service_vod服务的删除视频方法
            vodClient.removeAlyVideo(sourceId);
        }

        videoService.removeById(id);
        return R.ok();
    }

    //修改小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }

    //根据id查询小节
    @GetMapping("/getVideo/{videoId}")
    public R getVideo(@PathVariable String videoId){
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("myvideo",video);
    }
}

